package com.flin.icesilkworm.examination.api.impl;

import static com.flin.icesilkworm.framework.jdbc.objects.CollectionUtils.toMap;

import java.sql.Timestamp;
import java.util.Map;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.flin.icesilkworm.bgsys.api.ICommonApi;
import com.flin.icesilkworm.examination.api.IExaminationApi;
import com.flin.icesilkworm.examination.course.constant.ICourseLearnRecordDetailConstant;
import com.flin.icesilkworm.examination.course.constant.ICourseTeacherConstant;
import com.flin.icesilkworm.examination.course.model.CourseLearnRecord;
import com.flin.icesilkworm.examination.course.model.CourseLearnRecordDetail;
import com.flin.icesilkworm.examination.course.po.CourseLearnRecordDetailDo;
import com.flin.icesilkworm.examination.course.po.CourseLearnRecordDo;
import com.flin.icesilkworm.examination.course.po.CourseProjectsDo;
import com.flin.icesilkworm.framework.api.OpenApi;
import com.flin.icesilkworm.framework.logs.SybrisLog;

@OpenApi("exam")
@Component("exam")
@Transactional
public class ExaminationApiImpl implements IExaminationApi,ICommonApi{
	
	protected SybrisLog log = new SybrisLog(this.getClass());

	public SybrisLog getLog() {return log;}
	
	public Map<String, Object> learningRecord(long examid, CourseLearnRecord record,@OpenApi("proId") Long proId) {
		Long learnId = null;
		CourseLearnRecordDetail detail = null;
		if(record == null){
			learnId = CourseLearnRecordDo.set()
				.examId(examid)
				.proId(proId)
				.deleted(Boolean.FALSE)
				.lastedLearnTime(new Timestamp(System.currentTimeMillis()))
				.totalUsedTime(0)
				.re()
				.save();
		} else {
			learnId = record.getClrId();
			detail = CourseLearnRecordDetailDo.get().recordId(learnId)._orderByDesc(ICourseLearnRecordDetailConstant.DETAIL_ID).re().findForWhere();
		}
		return suc("返回结果",toMap("project",CourseProjectsDo.get().proId(proId).re()
				.and(t->t.joinCourseTeacherExampleByCourTeachId().setAttribute(ICourseTeacherConstant.TEACHER_NAME))
				.listForWhere().stream().findAny().orElse(null),"id",learnId,"lasted",detail));
	}
	
 
}
